package com.cskaoyan.shopping.api;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.dto.AllItemServiceResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SearchApiImpl {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ProductConverter productConverter;

    // 获得所有商品
    @GetMapping("/rpc/items")
    public AllItemServiceResponse getAllProductItem() {
        List<Item> items = itemMapper.selectAll();

        List<ItemDto> itemDtos = new ArrayList<>();
        for (Item item : items) {
            ItemDto itemDto = productConverter.item2ItemDto(item);
            itemDtos.add(itemDto);
        }

        AllItemServiceResponse allItemResponse = new AllItemServiceResponse();
        allItemResponse.setCallData(itemDtos);
        allItemResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        allItemResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return allItemResponse;

    }
}
